#!/bin/bash
# TDDTemplate.py            -- Python file which will be converted to FreeMarker Template
# it is enough to give the directory of the configuration file
# ./DejaVu.sh tmplFile [-regen] [ -cpr CopyRightFile] [-output tarFile] [-openCatia]
#=========================================
# Dependencies
#  	BitteGerne.sh 
#  	tmv shell script. Call tmv --help for details. MUST BE In /usr/bin and not /usr/local/bin
#  	ReplaceTextInFile.awk 
#=========================================

srcDir=tdd/src
targetDir=NEOForayWS
commonDir=../Common
dataDirName=data
frameworks="NEOTemplateUtilsFw  NEOUtilInterfaces"
targetFrameworks=( NEOForayWS  NEOForayWS NEOForayWS/NEOTemplateUIFw )

# Usage : CopyDir  src tgt
# Copies src dir to tgt dir without the .svn dirs. 
# If src already exist in tgt - it is removed before copying
CopyDir()
{
	# Delete the existing directories, if they exist
	dirToCopy=`basename $1`
	if [ -d  $2/$dirToDelete ] ; then 
		rm -rf $2/$dirToCopy
	fi

	cp -rf $1 -t $2

	# Delete the copied .svn directories
	#find $srcDir/$targetDir/$fw -name ".svn" -type d -exec rm -rf '{}' \;
	find $2/$dirToCopy -name ".svn" -type d | xargs rm -rf
}

#find . -type f -exec file '{}' \;
# Function to Copy the data files to src directory
CopyRequiredFiles()
{
	echo $dataDirName # Copying common data

	# Delete the previous data
	rm -rf $srcDir/$dataDirName
	
	# Copy the data files to src directory
	cp -rf $commonDir/$dataDirName $srcDir
	find $srcDir/$dataDirName -name ".svn" -type d | xargs rm -rf 		# Delete the copied .svn directories

	# Copy the Util Frameworks also
	for fw in $frameworks # for loop executing a command and iterating over the results
	do
		# Delete the existing directories, if they exist
		if [ -d  $srcDir/$targetDir/$fw ] ; then 
			rm -rf $srcDir/$targetDir/$fw
		fi
		
		cp -rf $commonDir/$targetDir/$fw $srcDir/$targetDir
		
		# Delete the copied .svn directories
		#find $srcDir/$targetDir/$fw -name ".svn" -type d -exec rm -rf '{}' \;
		find $srcDir/$targetDir/$fw -name ".svn" -type d | xargs rm -rf
	done
	
	# Copy the Addin module
	CopyDir $commonDir/$targetDir/NEOTemplateUIFw/NEOTemplateAddin.m $srcDir/$targetDir/NEOTemplateUIFw
	
	# Copy BitteGerneRun File to source directory
	cp -f $commonDir/BitteGerneRun.sh $srcDir

	cp -f $commonDir/BitteGerne* .
	cp -f $commonDir/Run.bat .
	cp -f $commonDir/GenTestCase.py .
	cp -f $commonDir/CAAUuidGen.py .

	find $commonDir/$targetDir -maxdepth 1 -type f -exec cp -f '{}' ${srcDir}/$targetDir \;
}

echo $0 ": Arguments : "$*

if [ ! -f $1 ]; then
echo $0 $LINENO " : First argument " $1 " is not a file or file doesn't exist." >& 2
exit $E_CONFFILE     # File not found error.
fi

openCatia=0
regen=0		# Regen to false
cprFile=""	# No copyright file
tmplFile=$1		# Template file
opTarFile=""
echo "Template file : " $tmplFile
shift       # Check next set of parameters.

while [ $# -gt 0 ]; do    # Until you run out of parameters . . .
  case "$1" in
    -regen)
              # "-regen" option
              regen=1
              ;;
    -openCatia)		# Open in CATIA              
              openCatia=1
               echo "Open in CATIA set TRUE"
              ;;
    -cpr)
              cprFile="$2"
              shift
              if [ ! -f $cprFile ]; then
                echo $LINENO " : Error: file" $cprFile " doesn't exist!"
                exit $E_CONFFILE     # File not found error.
              fi
			  if [ ! -n "$cprFile" ] ; then
                echo $LINENO " : No copyright file specified. A file must be specified with option -cpr"
                exit $E_CONFFILE     # File not found error.
			  fi
              echo " Reading copyright file " $cprFile
              ;;
    -output)
              opTarFile="$2"
              shift
			  if [ ! -n "$opTarFile" ] ; then
                echo "Output file name not specified. "
			  fi
              echo "Generated code will be in " $opTarFile
              ;;
  esac
  shift       # Check next set of parameters.
done

# Copy the data files from common directory to src directory
if [ $regen == 1 ] ; then
	CopyRequiredFiles
fi

# Copy the Copyright file to tdd/src/data directory
# if the first argument is NOT NULL and is a file then 
if [ -n "$cprFile" ] ; then
	cp -f $cprFile $srcDir/$dataDirName/copyright.txt
fi

cwd=$PWD
export cwd

# Create the dir where the Generated Code will be placed
odir=tdd/old

if [ -d  $odir ] ; then 
	rm -rf $odir
fi

cp -f $tmplFile TestCase.py

# Generate new UUID. cog.py must be installed.
#python /bin/cog.py -d -o TestCase.py $tmplFile
python CAAUuidGen.py -genIID >> TestCase.py

echo "if __name__ == '__main__':" >> TestCase.py
echo "    print Workspace"		  >> TestCase.py

#Run the generated TestCase.py file to dump the NewWorkspace Name
Workspace=`python TestCase.py`

# Change .py file to .tpl by adding {} and remove __main__  in last 2 lines.
echo "{" > Template.tpl
head -n -2 TestCase.py >> Template.tpl
echo "}" >> Template.tpl

# Replace = with colon and move the file to src dir
sed 's/=/:/g'  Template.tpl > tdd/src/data/test.tdd
  
##Run fmpp via batch file as Windows environment is needed by the fmpp.exe
./Run.bat

#Change to output directory
cd $odir

# Run the Generated shell file to call the BitterGerne.sh file - to rename the dirs and non text files ( e.g .bmp )
./BitteGerneRun.sh $cwd $tmplFile $SECONDS $openCatia $opTarFile 

cd $cwd

# Generate All the possible test cases.

echo "${odir}/${Workspace}"

python GenTestCase.py > ${odir}/${Workspace}/TestCases.txt

echo "Total Time Taken" $SECONDS "seconds"

